package com.fyzk.activity.lottery.jc.football;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.adapter.JCZQZhiFuBaseAdapter;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnFootballPayTeamDeleteListener;
import com.fyzk.net.PublicMethod;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

public class JCZQ_ZhiFuAdapter extends JCZQZhiFuBaseAdapter {
    private static final String TAG = "JC_ZhiFuAdapter";
    private boolean isdanguan;
    private LayoutInflater mInflater;
    private Handler handler;
    private int zhuShu;
    private OnFootballPayTeamDeleteListener listener;
    private int danCount = 0;
    private String chuanStr = JC_GuoGuanDialog.result;
    private Context context;

    public JCZQ_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler handler,
                             OnFootballPayTeamDeleteListener listener) {
        this.isdanguan = type;
        this.infors = getInfor(infors);
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        this.listener = listener;
        refreshDan();
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public Object getItem(int position) {
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.jc_zhifu_listview_item, null);
            holder.zhu = convertView.findViewById(R.id.jc_zhifu_list_item_zhu);
            holder.delete = convertView.findViewById(R.id.jc_zhifu_list_item_delete);
            holder.sheng1 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_sheng1);
            holder.ping1 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_ping1);
            holder.fu1 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu1);
            holder.week_num = convertView.findViewById(R.id.week_num);
            holder.sheng2 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_sheng2);
            holder.ping2 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_ping2);
            holder.fu2 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu2);
            holder.sheng2_r = convertView.findViewById(R.id
                    .jc_zhifu_list_item_tv_sheng2_r);
            holder.ping2_r = convertView.findViewById(R.id
                    .jc_zhifu_list_item_tv_ping2_r);
            holder.fu2_r = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu2_r);
            holder.txt_rf = convertView.findViewById(R.id.txt_rf);
            holder.dan = convertView.findViewById(R.id.jc_zhifu_list_item_dan);
            holder.jz_ll_spf = convertView.findViewById(R.id.jz_ll_spf);
            holder.jz_stop_spf = convertView.findViewById(R.id.jz_stop_spf);
            holder.jz_ll_rspf = convertView.findViewById(R.id.jz_ll_rspf);
            holder.jz_stop_rspf = convertView.findViewById(R.id.jz_stop_rspf);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCInfo infor = infors.get(position);
        if (isdanguan) {
            holder.dan.setVisibility(View.GONE);
        }else{
            holder.dan.setVisibility(View.VISIBLE);
            holder.dan.setChecked(infor.isDan());
            if (infor.isDaneble()) {
                holder.dan.setEnabled(true);
                holder.dan.setTag(R.string.jc_info, infor);
                holder.dan.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                        infor.setDan(!infor.isDan());
                        refreshDan();
                    }
                });
            } else {
                holder.dan.setEnabled(false);
            }

        }
        holder.zhu.setVisibility(View.VISIBLE);
        holder.sheng1.setText(infor.getHomeTeam());
        holder.fu1.setText(infor.getAwayTeam());
        JCInfo.SPInfo spInfo_s = infor.btnHGPojo.get(infor.VS_3);
        JCInfo.SPInfo spInfo_p = infor.btnHGPojo.get(infor.VS_1);
        JCInfo.SPInfo spInfo_f = infor.btnHGPojo.get(infor.VS_0);
        JCInfo.SPInfo spInfo_rs = infor.btnHGPojo.get(infor.LETVS_3);
        JCInfo.SPInfo spInfo_rp = infor.btnHGPojo.get(infor.LETVS_1);
        JCInfo.SPInfo spInfo_rf = infor.btnHGPojo.get(infor.LETVS_0);
        holder.ping1.setText("vs");
        holder.sheng2_r.setText("让胜" + infor.getLetvs_3());
        holder.ping2_r.setText("让平" + infor.getLetvs_1());
        holder.fu2_r.setText("让负" + infor.getLetvs_0());
        holder.ping1.setText("vs");
        holder.sheng2.setText("胜" + infor.getVs_3());
        holder.ping2.setText("平" + infor.getVs_1());
        holder.fu2.setText("负" + infor.getVs_0());
        holder.txt_rf.setText("让\n" + infor.getHandicap());
        if (isdanguan) {
            if (infor.getDgStaticSaleSpfWrqStatus() == 1) {//胜平负单关停售
                holder.jz_ll_spf.setVisibility(View.GONE);
                holder.jz_stop_spf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_spf.setVisibility(View.VISIBLE);
                holder.jz_stop_spf.setVisibility(View.GONE);
            }
            if (infor.getDgStaticSaleSpfStatus() == 1) {//rq胜平负单关停售
                holder.jz_ll_rspf.setVisibility(View.GONE);
                holder.jz_stop_rspf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_rspf.setVisibility(View.VISIBLE);
                holder.jz_stop_rspf.setVisibility(View.GONE);
            }
        } else {//过关
            if (infor.getStaticSaleSpfWrqStatus() == 1) {//胜平负过关停售
                holder.jz_ll_spf.setVisibility(View.GONE);
                holder.jz_stop_spf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_spf.setVisibility(View.VISIBLE);
                holder.jz_stop_spf.setVisibility(View.GONE);
            }
            if (infor.getStaticSaleSpfStatus() == 1) {//rq胜平负过关停售
                holder.jz_ll_rspf.setVisibility(View.GONE);
                holder.jz_stop_rspf.setVisibility(View.VISIBLE);
            } else {
                holder.jz_ll_rspf.setVisibility(View.VISIBLE);
                holder.jz_stop_rspf.setVisibility(View.GONE);
            }
        }
        if (infor.getHandicap().contains("+")) {
            holder.txt_rf.setBackgroundColor(context.getResources().getColor(R.color.jczq_hh_l_bg));
        } else {
            holder.txt_rf.setBackgroundColor(context.getResources().getColor(R.color.jczq_hh_r_bg));
        }
        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor
                .getMatchNum()));
        holder.sheng2.setOnCheckedChangeListener(null);
        holder.fu2.setOnCheckedChangeListener(null);
        holder.ping2.setOnCheckedChangeListener(null);
        holder.sheng2_r.setOnCheckedChangeListener(null);
        holder.fu2_r.setOnCheckedChangeListener(null);
        holder.ping2_r.setOnCheckedChangeListener(null);
        holder.sheng2.setChecked(spInfo_s.isSel);
        holder.fu2.setChecked(spInfo_f.isSel);
        holder.ping2.setChecked(spInfo_p.isSel);
        holder.sheng2_r.setChecked(spInfo_rs.isSel);
        holder.fu2_r.setChecked(spInfo_rf.isSel);
        holder.ping2_r.setChecked(spInfo_rp.isSel);
        holder.sheng2.setEnabled(spInfo_s.isEnadble);
        holder.fu2.setEnabled(spInfo_f.isEnadble);
        holder.ping2.setEnabled(spInfo_p.isEnadble);
        holder.sheng2_r.setEnabled(spInfo_rs.isEnadble);
        holder.fu2_r.setEnabled(spInfo_rf.isEnadble);
        holder.ping2_r.setEnabled(spInfo_rp.isEnadble);
        holder.sheng2.setTag(spInfo_s);
        holder.sheng2.setTag(R.string.jc_info, infor);
        holder.sheng2.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.ping2.setTag(spInfo_p);
        holder.ping2.setTag(R.string.jc_info, infor);
        holder.ping2.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.fu2.setTag(spInfo_f);
        holder.fu2.setTag(R.string.jc_info, infor);
        holder.fu2.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.sheng2_r.setTag(spInfo_rs);
        holder.sheng2_r.setTag(R.string.jc_info, infor);
        holder.sheng2_r.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.ping2_r.setTag(spInfo_rp);
        holder.ping2_r.setTag(R.string.jc_info, infor);
        holder.ping2_r.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.fu2_r.setTag(spInfo_rf);
        holder.fu2_r.setTag(R.string.jc_info, infor);
        holder.fu2_r.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.delete.setTag(R.string.jc_info, infor);
        holder.delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDelete((JCInfo) v.getTag(R.string.jc_info));
                }
                JCBasePayActivity.infos.remove(position);
                setInfors(JCBasePayActivity.infos, isdanguan);
            }
        });
        jiSuanZhuShu();
        return convertView;
    }


    private void selChange(JCInfo.SPInfo spInfo, JCInfo info, boolean isChecked) {
        spInfo.isSel = isChecked;
        updateTouMsg(info, isChecked);
    }

    CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton
            .OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            selChange((JCInfo.SPInfo) buttonView.getTag(), (JCInfo) buttonView.getTag(R.string
                    .jc_info), isChecked);
        }
    };

    private void updateTouMsg(JCInfo infor, boolean isChecked) {
        if (isChecked) {
            infor.setOnclikNum(infor.getOnclikNum() + 1);
        } else {
            infor.setOnclikNum(infor.getOnclikNum() - 1);
        }
        if (infor.getOnclikNum() == 0) {
            if (listener != null) {
                listener.onDelete(infor);
            }
        }
        jiSuanZhuShu();
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCInfo> getInfor(List<JCInfo> infors) {
        ArrayList<JCInfo> list = new ArrayList<JCInfo>();
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() > 0) {
                list.add(info);
            }
        }
        JCBasePayActivity.infos = list;
        return list;
    }

    /**
     * 计算注数（待修改） ///苗强 ---暂不支持 多串过关  只支持 自由过关
     */
    public void jiSuanZhuShu() {
        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();
        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        boolean isDanShi = true;
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() != 0) {
                changCi++;
                if (info.getOnclikNum() != 1) {
                    isDanShi = false;
                }
            }
            clicknums.add(info.getOnclikNum());
            if (info.isDan()) {
                wanFa = 2;
            }
            dannums.add(info.isDan());// 添加胆拖
        }

        List<Integer> chuans = new ArrayList<Integer>();
        if (isdanguan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            String chuan = JC_GuoGuanDialog.result;
            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }

                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);

                String betcode = JZBetContentUtil.getContentSPF(infors, isdanguan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isdanguan, dannums);
            }
        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }

    public List<JCInfo> getInfors() {
        return infors;
    }

    public void setInfors(List<JCInfo> infors, boolean isDanguan) {
        this.isdanguan = isDanguan;
        this.infors = infors;
        this.notifyDataSetChanged();
        if(infors.size()==0){
            jiSuanZhuShu();
        }
    }

    class ViewHolder {
        LinearLayout zhu;
        LinearLayout delete;
        LinearLayout jz_ll_spf;
        TextView jz_stop_spf;
        LinearLayout jz_ll_rspf;
        TextView jz_stop_rspf;
        TextView sheng1;
        TextView week_num;
        TextView ping1;
        TextView fu1;
        TextView txt_rf;
        CheckBox sheng2;
        CheckBox ping2;
        CheckBox fu2;
        CheckBox sheng2_r;
        CheckBox ping2_r;
        CheckBox fu2_r;
        CheckBox dan;

    }

}
